package twoD.IsoPfem;

import inf.jlinalg.MatrixFormat;
import inf.jlinalg.Vector2D;

public class Node {
	private int[] dofNumbers_;
	private Vector2D position_;
	private Constraint constraint_;
	private Force f_;
	private Force nodalF_;
	private Vector2D displacement_;
	private int id;

	public Node(Vector2D position) {
		position_ = position;
		dofNumbers_ = new int[2];
	}

	public Node(double... x) {
		this(new Vector2D(x[0], x[1]));
	}


	public void setId(int i) {
		id = i;
	}

	public int getId() {
		return id;
	}

	public void setConstraint(Constraint c) {
		constraint_ = c;
	}

	public Constraint getConstraint() {
		return constraint_;
	}

	public void setForce(Force f) {
		f_ = f;
	}

	@Deprecated
	public void setNodalForce(Force f) {
		nodalF_ = f;
	}

	public void addNodalForce(double... f) {
		if (nodalF_ != null)
			nodalF_.add(f);
		else
			nodalF_ = new Force(f);
	}

	public Force getForce() {
		return f_;
	}

	public Force getNodalForce() {
		return nodalF_;
	}

	public Vector2D getForceVector() {
		if (f_ != null)
			return new Vector2D(f_.getComponent(0), f_.getComponent(1));
		else
			return new Vector2D(0.0, 0.0);
	}

	/*
	 * the nodal force vector coming out from edge load/body load act on element
	 * by which it's propagated to the node
	 */
	public Vector2D getNodalForceVector() {
		if (nodalF_ != null)
			return new Vector2D(nodalF_.getComponent(0),
					nodalF_.getComponent(1));
		else
			return new Vector2D(0.0, 0.0);
	}

	public Vector2D getPosition() {
		return position_;
	}

	public void setDisplacement(double[] u) {
		displacement_ = new Vector2D(u);
	}

	public void updateDisplacement(double[] du) {
		displacement_ = displacement_.add(new Vector2D(du));
	}

	public Vector2D getDisplacement() {
		return displacement_;
	}

	public int enumerateDOFs(int start) {
		for (int i = 0; i < dofNumbers_.length; i++)
			if (constraint_ != null)
				if (constraint_.isFree(i))
					dofNumbers_[i] = start++;
				else
					dofNumbers_[i] = -1;
			else
				dofNumbers_[i] = start++;
		return start;
	}

	public int[] getDofNumbers() {
		return dofNumbers_;
	}

	public void printPosition() {
		System.out.print(MatrixFormat.format(position_));
	}

	public void printDisplacement() {
		System.out.print(MatrixFormat.format(displacement_));
	}

	public void printConstraint() {
		if (constraint_ == null)
			System.out.print("free\tfree\tfree");
		else
			constraint_.print();
	}

	public void printAll() {
		printPosition();
		printDisplacement();
	}

}
